package com.cj.webapp_Start.sp_network.encryption;


import android.os.Build;
import android.util.Base64;

import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AESUtils {

    public static void main(String[] args) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            try {
                getBase64Key();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 加密算法RSA
     */
    public static final String KEY_ALGORITHM = "AES";
    /**
     * 填充模式(jdk默认)
     */
    public static final String PADDING = "AES/CBC/PKCS7Padding";


    /**
     * 生成密钥
     *
     * @return
     * @throws Exception
     */
    public static String getBase64Key() throws Exception {
        KeyGenerator keyGen = KeyGenerator.getInstance(KEY_ALGORITHM);
        SecureRandom secureRandom = new SecureRandom(
                String.valueOf(System.currentTimeMillis()).getBytes(StandardCharsets.UTF_8));
        keyGen.init(256, secureRandom);
        SecretKey secretKey = keyGen.generateKey();
        byte[] key = secretKey.getEncoded();
        return Base64.encodeToString(key, Base64.NO_WRAP);
    }

    /**
     * AES加密
     *
     * @param oriData
     * @param base64Key
     * @return
     * @throws Exception
     */
    public static String encrypt(String oriData, String base64Key) throws Exception {
        byte[] dataBytes = oriData.getBytes(StandardCharsets.UTF_8);
        byte[] keyBytes = checkKeys(base64Key);

        byte[] ivBytes = new byte[16];
        System.arraycopy(keyBytes, 0, ivBytes, 0, 16);
        Cipher cipher = Cipher.getInstance(PADDING);
        if (keyBytes.length == 0) {

        }
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyBytes, KEY_ALGORITHM),
                new IvParameterSpec(ivBytes));
        byte[] bytes = cipher.doFinal(dataBytes);
        return Base64.encodeToString(bytes, Base64.NO_WRAP);
    }

    private static byte[] checkKeys(String base64Key) {
        byte[] keys = Base64.decode(base64Key.getBytes(StandardCharsets.UTF_8), Base64.NO_WRAP);
        if(keys.length==0){
            keys = Base64.decode("abcdefghijklmnopqqrsttvvyzaabm".getBytes(StandardCharsets.UTF_8), Base64.NO_WRAP);
        }
        return keys;
    }

    /**
     * AES解密
     *
     * @param base64Data
     * @param base64Key
     * @return
     * @throws Exception
     */

    public static String decrypt(String base64Data, String base64Key) throws Exception {
        byte[] dataBytes = Base64.decode(base64Data, Base64.NO_WRAP);
        byte[] keyBytes =  checkKeys(base64Key);
        byte[] ivBytes = new byte[16];
        System.arraycopy(keyBytes, 0, ivBytes, 0, 16);
        Cipher cipher = Cipher.getInstance(PADDING);
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyBytes, KEY_ALGORITHM),
                new IvParameterSpec(ivBytes));
        byte[] bytes = cipher.doFinal(dataBytes);
        String res = new String(bytes, StandardCharsets.UTF_8);
        return res;
    }

    /**
     * 获取IV
     *
     * @param base64Key
     * @return
     */
    public static String getIvFromKey(String base64Key) {
        byte[] keyBytes = checkKeys(base64Key);
        byte[] ivBytes = new byte[16];
        System.arraycopy(keyBytes, 0, ivBytes, 0, 16);
        return Base64.encodeToString(ivBytes, Base64.NO_WRAP);
    }
}
